import Vuex, { Commit } from 'vuex'
import {
  getExpressTypes,
  getGoodsTypes,
  getSaleTypes,
  getTypesItem,
} from '@/api/modules/shop'
interface expressType {
  label: string
  value: string
}

interface State {
  expressTypes: expressType[]
  shopTypes: any[]
}
const state: State = {
  expressTypes: [],
  shopTypes: [],
}

const mutations = {
  // qrcodeShowData
  SET_EXPRESSTYPES: (state: State, types: expressType[]) => {
    console.log('store types' + types)
    state.expressTypes = types
  },
  SET_LOADTYPES: (state: State, shopTypes: any) => {
    state.shopTypes = shopTypes
  },
}

const actions = {
  setExpressTypes({ commit }: { commit: Commit }) {
    console.log('setExpressTypes')
    // commit('SET_EXPRESSTYPES')
    getExpressTypes()
      .then((res) => {
        commit('SET_EXPRESSTYPES', res.data)
      })
      .catch(() => {})
  },
  loadTypes({ commit }: { commit: Commit }) {
    const search: getTypesItem = { status: 1 }
    return new Promise((resolve, reject) => {
      Promise.all([getGoodsTypes(search), getSaleTypes(search)])
        .then((res) => {
          const data = [
            { name: '商品分类', id: 1, children: res[0].data },
            { name: '营销分类', id: 2, children: res[1].data },
          ]
          commit('SET_LOADTYPES', data)
          resolve(data)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
}

export default {
  namespaed: true,
  state,
  mutations,
  actions,
}
